package com.tjp.eight.ui.fragment.rank;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.comment.AppManager;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.ui.fragment.tabFind.FindPostModel;
import com.tjp.eight.ui.fragment.tabFind.FindResultModel;
import com.tjp.eight.utils.ActivityUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

/**
 * Created by Administrator on 2017/2/17.
 */

public class FragmentRank extends BaseFragment<RankPresenter> implements RankView {
    private int cPage = 1;
    private RecyclerView recyclerView;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private View footView;
    private RankAdapter rankAdapter;
    private ImageView iv_head_left;
    private RelativeLayout rlHeadLeft;
    private TextView tvTitle;
    private String rankType = "";

    @Override
    protected RankPresenter createPresenter() {
        return new RankPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.from(getContext()).inflate(R.layout.fragment_rank, container, false);
        }
        initViews();
        return contentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter.loadData(getJson(rankType));
    }

    @Override
    public void loadData(FindResultModel findResultModel) {
        if (findResultModel == null) return;
        if (IsSuccessUtils.isSuccess(findResultModel.result)) {
            rankAdapter.addData(findResultModel.data);
            if (rankAdapter.getItemCount() == findResultModel.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        } else {
            ToatUtils.toastCommentFail(getContext(), findResultModel.msg);
        }
    }

    @Override
    public void loadFail(String msg) {
        ToatUtils.toastCommentFail(getContext(), msg);
    }

    private String getJson(String rankType) {
        FindPostModel findPostModel = new FindPostModel();
        findPostModel.order = rankType;
        findPostModel.page = cPage;
        findPostModel.uid = SharePreferencesUtils.getUid();
        findPostModel.loginkey = SharePreferencesUtils.getLoginKey();
        return new Gson().toJson(findPostModel);
    }

    private Bitmap getBitmap(int id) {
        return BitmapFactory.decodeResource(getResources(), id);
    }

    public void setRankType(String rankType) {
        this.rankType = rankType;
    }

    void initViews() {
        iv_head_left = (ImageView) contentView.findViewById(R.id.iv_head_left);
        rlHeadLeft = (RelativeLayout) contentView.findViewById(R.id.rl_head_left);
        rlHeadLeft.setVisibility(View.VISIBLE);
        tvTitle = (TextView) contentView.findViewById(R.id.tv_title);
        if ("day".equals(rankType)) {
            tvTitle.setText("本日排行");
        } else if ("week".equals(rankType)) {
            tvTitle.setText("本周排行");
        } else {
            tvTitle.setText("本月排行");
        }
        Bitmap bitmap = getBitmap(R.drawable.top_back);
        iv_head_left.setImageBitmap(bitmap);
        recyclerView = (RecyclerView) contentView.findViewById(R.id.rv_rank);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        rankAdapter = new RankAdapter(getActivity());
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(rankAdapter);
        rankAdapter.setItemOnClick(new RankAdapter.RankItemOnClick() {
            @Override
            public void itemClick(String cid) {
                ActivityUtils.startDetails(getActivity(), cid);
            }
        });
        footView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_refresh_footer, recyclerView, false);
        headerViewRecyclerAdapter.addFooterView(footView);
        recyclerView.setAdapter(headerViewRecyclerAdapter);
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
                cPage = currentPage;
                presenter.loadData(getJson(rankType));
            }
        });
        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            }
        });
        rlHeadLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity(getActivity());
            }
        });

    }
}
